package edu.pku.se.chocolate.UI.View;

import java.util.ArrayList;

import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;
import edu.pku.se.chocolate.UI.Adapter.InformationListAdapter;
import edu.pku.se.chocolate.UI.Adapter.TagRowAdapter;
import edu.pku.se.chocolate.UI.widget.TagGallery;
import edu.pku.se.chocolate.datastructure.Category;
import edu.pku.se.chocolate.datastructure.Tag;


public class InformationView extends ChocolateView implements OnItemClickListener{

	private TextView title;
	private Category myself;
	private int choosedButton = 0;
	private TagGallery tagLayout;
//	private LinearLayout mainLayout;
	TagRowAdapter tap;
	private ExpandableListView il;
	private InformationListAdapter ila;
	private InformationView thisSelf = this;
	private Handler myHandler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			thisSelf.show();
			return true;
		}
	});
	private Handler moreHandler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			//thisSelf.show();
			ila.addMessage();
			return true;
		}
	});
	private Handler numHandler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			thisSelf.tagShow();
			return true;
		}
	});
/*
 * add tag: 	
 */
	public AddpreDefTagView apdtv;
	public AddselfDefTagView asdtv;
	
	
	public InformationView(View self,Category myself) {
		super(self);
		LinearLayout llup = (LinearLayout) this.self.findViewById(R.id.linearLayout_inf_main);
		llup.setBackgroundResource(R.drawable.bg);
		llup = (LinearLayout) this.self.findViewById(R.id.linearLayoutup);
//		AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, (120*ChocolateActivity.DM.heightPixels)/480);
//		llup.setBackgroundColor(0xff8b4513);
//		llup.setLayoutParams(layoutParams);
		llup.setBackgroundResource(R.drawable.bg5);
		this.deleteVisible = false;
		//所属类别
		this.myself = myself;
		//标题
		title = (TextView) self.findViewById(R.id.textView_title);
		//title.setText(this.myself.name);
		title.setText(myself.getDisplayName());
		title.setTextColor(0xff000000);
		//标签列表
		tagLayout =  (TagGallery) this.self.findViewById(R.id.tagGallery1);
		tap = new TagRowAdapter(this.myself,mainActivity,this);
		tagLayout.setAdapter(tap);
		tagLayout.setBackgroundResource(R.drawable.tagbg);
		il = (ExpandableListView) this.self.findViewById(R.id.expandableListView1);
		//开始时标签选中为全部
//		if(tap.getTag(choosedButton)!=null)
			ila = new InformationListAdapter(mainActivity,tap.getTag(choosedButton),myHandler,moreHandler);
//		else
//			ila = new InformationListAdapter(mainActivity,null,myHandler,moreHandler);
		il.setAdapter(ila);
		int screenWidth = ChocolateActivity.DM.widthPixels;
		il.setIndicatorBounds((screenWidth-(screenWidth*45)/480), screenWidth);
		il.setOnGroupExpandListener(new OnGroupExpandListener(){

			@Override
			public void onGroupExpand(int arg0) {
				// TODO Auto-generated method stub
				for(int i=0;i<ila.getGroupCount();i++) {
					if(arg0!=i)
						il.collapseGroup(i);
				}
			}
			
		});
		il.setCacheColorHint(0x00000000);
		il.setBackgroundResource(R.drawable.bg);
//		il.setBackgroundColor(0xff000000);
//		tagLayout.setBackgroundColor(0xff8b4513);
		init();	
		
	}
	
	//初始化标签行
	private void init() {
//		tagLayout.setOnItemSelectedListener(this);
		tagLayout.setOnItemClickListener(this);
		tagLayout.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_UP){
//					choosedButton = arg2;
					tap.getTag(choosedButton).new_msg_num = 0;
					ChocolateActivity.c_solver.clearTagNewMsgNum(tap.getTag(choosedButton));
//					tap.notifyDataSetChanged(arg2);//arg2会返回当前选中项的位置，调用此方法，通知更新
					ila = new InformationListAdapter(mainActivity,tap.getTag(choosedButton),myHandler,moreHandler);
					il.setAdapter(ila);
				} 
				if(event.getAction() == MotionEvent.ACTION_DOWN){
//					update.setBackgroundColor(android.graphics.Color.YELLOW);
				}
				return false;
			}
		});
		tagLayout.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				choosedButton = arg2;
				tap.notifyDataSetChanged(arg2);//arg2会返回当前选中项的位置，调用此方法，通知更新
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		addtag_init();
		setAddTagButtonListener(self);
		setFlushButton();
	}
	
	private void addtag_init()
	{
		View sav = ChocolateActivity.inflate.inflate(R.layout.selfadd, null);
		View pav = ChocolateActivity.inflate.inflate(R.layout.preadd, null);
		//保证category已经赋值！
		asdtv = new AddselfDefTagView(sav, myself);
		apdtv = new AddpreDefTagView(pav, myself);
		asdtv.setSelfButtonListener(sav);
		apdtv.preButtonListener(pav);
		
		TextView top = (TextView)pav.findViewById(R.id.preToptv);
		top.setGravity(Gravity.CENTER);		
	}

/*	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		tap.notifyDataSetChanged(arg2);//arg2会返回当前选中项的位置，调用此方法，通知更新
		choosedButton = arg2;
		tap.getTag(choosedButton).new_msg_num = 0;
		ila = new InformationListAdapter(mainActivity,tap.getTag(choosedButton),myHandler,moreHandler);
		il.setAdapter(ila);
	}
	

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}*/
	
	public void setAddTagButtonListener(View _v)
	{
		Button bt_addtag = (Button)_v.findViewById(R.id.button_sh_2);
//		bt_addtag.setBackgroundColor(0xff8b4513);
//		bt_addtag.setTextColor(android.graphics.Color.WHITE);
		bt_addtag.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(myself.isCustomCategory())//Modify By JHT
				{
					asdtv.selfInitial();
					asdtv.show();
				}
				else
				{
					apdtv.preInitial();
					apdtv.show();
				}
			}
			
		});
	}

	public void setFlushButton() {
		Button flush = (Button) this.self.findViewById(R.id.button_sh_1);
//		flush.setBackgroundColor(0xff8b4513);
//		flush.setTextColor(android.graphics.Color.WHITE);
		flush.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ila.changeFlush();
				ArrayList<Tag> temp_tags = ChocolateActivity.c_solver.getTagList(myself);
				if(temp_tags.size()>0)
					ChocolateActivity.c_solver.requestLatestMessage(temp_tags.get(choosedButton), myHandler);
			}
			
		});
	}
	
	public void show() {
		tap = new TagRowAdapter(this.myself,mainActivity,this);
		tagLayout.setAdapter(tap);
		ila = new InformationListAdapter(mainActivity,tap.getTag(choosedButton),myHandler,moreHandler);
		il.setAdapter(ila);
		tagLayout.setSelection(choosedButton);
//		if (ChocolateActivity.view_stack.peek() != this)
		ChocolateActivity.c_solver.setautoReflash(numHandler,
				ChocolateActivity.c_solver.getTagList(myself));
		super.show();
	}
	
	private void tagShow() {
		tap = new TagRowAdapter(this.myself,mainActivity,this);
		tagLayout.setAdapter(tap);
		tagLayout.setSelection(choosedButton);
	}
	
	public TagRowAdapter getTagRow() {
		return tap;
	}
	
	public void setDeleteVisible(boolean d) {
		this.deleteVisible = d;
	}
	
	public void deleteTag() {
		choosedButton = 0;
		tap.delete();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		choosedButton = arg2;
		tap.getTag(choosedButton).new_msg_num = 0;
		ChocolateActivity.c_solver.clearTagNewMsgNum(tap.getTag(choosedButton));
		tap.notifyDataSetChanged(arg2);//arg2会返回当前选中项的位置，调用此方法，通知更新
		ila = new InformationListAdapter(mainActivity,tap.getTag(choosedButton),myHandler,moreHandler);
		il.setAdapter(ila);
	}
	
	public int getChoosedButton() {
		return choosedButton;
	}
	
	public boolean isCustom() {
		return myself.isCustomCategory();
	}
}
